<template>
  <div>
    <el-form ref="pram" :model="pram" :rules="rules" label-width="100px" @submit.native.prevent>
      <el-form-item label="管理员账号" prop="username">
        <el-input v-model="pram.username" maxlength="32" placeholder="管理员账号" />
      </el-form-item>
      <el-form-item label="管理员密码" prop="password">
        <el-input
          maxlength="16"
          v-model="pram.password"
          type="password"
          placeholder="管理员密码"
          clearable
          @input="handlerPwdInput"
          @clear="handlerPwdInput"
        />
      </el-form-item>
      <el-form-item v-if="pram.password" label="确认密码" prop="repwd">
        <el-input v-model="pram.repwd" maxlength="16" type="password" placeholder="确认密码" clearable />
      </el-form-item>
      <el-form-item label="名称" prop="real_name">
        <el-input v-model="pram.real_name" maxlength="12" placeholder="管理员名称" />
      </el-form-item>
      <el-form-item label="管理员身份" prop="roles">
        <el-select v-model="pram.roles" placeholder="身份" clearable style="width: 100%">
          <el-option
            v-for="(item,index) in roleList"
            :key="index"
            :label="item.role_name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态">
        <el-switch v-model="pram.status" :active-value="1" :inactive-value="0" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handlerSubmit('pram')">{{ isCreate===0?'确定':'更新' }}</el-button>
        <el-button @click="close">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import * as systemAdminApi from '@/api/systemadmin.js'
import {Debounce} from '@/utils/validate'
import {saveAdmin} from "@/api/systemadmin.js";
export default {
  // name: "edit"
  components: { },
  props: {
    isCreate: {
      type: Number,
      required: true
    },
    editData: {
      type: Object,
      default: () => {
        return { rules: [] }
      }
    },
    allRoleList: {
      type: Array
    }
  },
  data() {
    const validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.pram.password) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      constants:this.$constants,
      pram: {
        username: null,
        password: null,
        repwd: null,
        real_name: null,
        roles: null,
        status: 1,
        id: 0,
      },
      roleList: [],
      rules: {
        username: [{ required: true, message: '请填管理员账号', trigger: ['blur', 'change'] }],
        password: [{ required: true, message: '请填管理员密码', trigger: ['blur', 'change'] }],
        repwd: [{ required: true, message: '确认密码密码', validator: validatePass, trigger: ['blur', 'change'] }],
        real_name: [{ required: true, message: '管理员名称', trigger: ['blur', 'change'] }],
        roles: [{ required: true, message: '管理员身份', trigger: ['blur', 'change'] }],
      },
    }
  },
  mounted() {
    this.initEditData()
  },
  methods: {
    close() {
      this.$emit('hideEditDialog')
    },
    initEditData() {
      this.roleList = this.allRoleList

      if (this.isCreate !== 1) return //编辑
      const { username, real_name, roles, status, id } = this.editData
      this.pram.username = username
      this.pram.real_name = real_name
      this.pram.roles = roles
      this.pram.status = status
      this.pram.id = id
      this.rules.password = []
      this.rules.repwd = []
    },
    handlerSubmit:Debounce(function(form) {
      this.$refs[form].validate(valid => {
        if (!valid) return
        systemAdminApi.saveAdmin(this.pram).then(data => {
          this.$message.success('保存成功')
          this.$emit('hideEditDialog')
        })
      })
    }),
    handlerPwdInput(val) {
      if (!val) {
        this.rules.password = []
        this.rules.repwd = []
        return
      }
      this.rules.password = [
        { required: true, message: '请填管理员密码', trigger: ['blur', 'change'] },
        { min: 6, max: 20, message: '长度6-20个字符', trigger: ['blur', 'change'] }]
      this.rules.repwd = [{ required: true, message: '两次输入密码不一致', validator: (rule, value, callback) => {
        if (value === '') {
          callback(new Error('两次输入密码不一致!'))
        } else if (value !== this.pram.password) {
          callback(new Error('两次输入密码不一致!'))
        } else {
          callback()
        }
      }, trigger: ['blur', 'change'] }]
    }
  }
}
</script>

<style scoped>

</style>
